﻿using Microsoft.Speech.Synthesis;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Speech.AudioFormat;
using System.Configuration;
using System.Threading;
using Syntezator_mowy.Extras;
using Syntezator_mowy.Extras.Exception;
using Syntezator_mowy.DataReader;

namespace Syntezator_mowy.Tts
{
    public class Tts
    {
        private ParametrySyntezy parametry;
        public Tts(ParametrySyntezy parametry) 
        {
            this.parametry = parametry;
        }

        public void run()
        {
            string baseName;
            string extension;
            splitFileName(parametry, out baseName, out extension);

            //int iloscPlikow= int.Parse(ConfigurationManager.AppSettings["syntezator_fileBuffer"].ToString());

            SynchrBuffer<string> dataBuffer = new SynchrBuffer<string>();

            SynchrBuffer<string> fileBuffer = parametry.buforNaPliki;

            ParametryLaczenia dataReaderParams = new ParametryLaczenia(ref dataBuffer, parametry.sciezkaDoPlikuWejsciowego);

            Syntezator_mowy.DataReader.DataReader dataReader = new Syntezator_mowy.DataReader.DataReader(dataReaderParams);
            Thread dataReaderThread = new Thread(new ThreadStart(dataReader.run));
        
            TtsActionModule actionModule = new TtsActionModule(parametry.jezykPrzetwarzania,
                ref fileBuffer, 
                ref dataBuffer,
                baseName, extension);
            Thread actionModuleThread = new Thread(new ThreadStart(actionModule.run));

            dataReaderThread.Start();
            actionModuleThread.Start();

            dataReaderThread.Join();
            actionModule.EndAddingData = true;
            actionModuleThread.Join();                
        }

        private static void splitFileName(ParametrySyntezy parametry, out string baseName, out string extension)
        {
            string[] splitedFile = parametry.sciezkaDoPlikuWyjsciowego.Split('.');

            baseName = splitedFile[0];

            extension = "";
            if (splitedFile.Length > 1)
            {
                extension = splitedFile[1];
            }
            else
            {
                throw new eNoExtension();
            }
        }
    }
}
